import { AxiosInstance } from "axios";
import axios from 'axios'

const instance: AxiosInstance = axios.create({
  baseURL: 'http://localhost:8081/',
  timeout: 2000,
  headers: {
    'Content-type': 'application/json',
    // 'Access-Control-Allow-Origin': 'http://localhost:8080',
    // 'Access-Control-Allow-Credentials': 'true'
  },
  withCredentials: true
})

class UserService {
  static async register(user: User) {
    let response = await instance.post('register/user', JSON.stringify(user))
    return response.data
  }

  static async loginUser(user: User) {
    // TODO
    let response = await instance.post('login/user', JSON.stringify(user))
    return response.data
  }

  static async loginAdmin(user: User) {
    let response = await instance.post('login/admin', JSON.stringify(user))
    return response.data
  }

  static async getLoginUser() {
    let response = await instance.get('/login/user')
    return response.data
  }

  static async exit() {
    let response = await instance.delete('/login')
    return response.data
  }

  static async hasUser(name: string) {
    let response = await instance.get(`/user/${name}`)
    return response.data
  } 
  // static async islogin() {
  //   let response = await instance.get('/islogin')
  //   return response.data
  // }

  // static async findUser(name: string) {
  //   let response = await instance.get('user', {
  // 	params: {name}
  //     }
  //   )

  //   return response.data
  // }
}

export default UserService